package top.qitun.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.zhangxd.platform.common.web.util.BaitunWebUtils;
import io.swagger.annotations.ApiOperation;
import top.qitun.bizz.shop.tshop.entity.TShop;
import top.qitun.bizz.shop.tshop.entity.TShopBO;
import top.qitun.bizz.shop.tshop.service.TShopService;
import top.qitun.common.api.gateway.GatewayServiceAPI;
import top.qitun.modal.request.ShopCreateRequest;
import top.qitun.model.response.ObjectCollectionResponse;
import top.qitun.model.response.ObjectDataResponse;
import top.qitun.util.DateUtil;
import top.qitun.util.OrikaMapper;

/**
 * 店铺创建类
 * @author Cloudwei
 *
 */
@RefreshScope
@RestController
@RequestMapping(value = "/v1")
public class ShopController {
	Logger logger = LoggerFactory.getLogger(getClass());
	
    @Autowired
    private TShopService shopService;
    
    @Autowired
   	private GatewayServiceAPI gatewayServiceAPI;
    
    @Value("${abc.test}")
   	public String confValue;
   	
   	@RequestMapping(value = "testtime", method = RequestMethod.GET)
   	public String create() {
   		return confValue;
   	}

    @ApiOperation(value = "创建店铺", notes = "用户创建新店铺")
    @RequestMapping(value = "/shop", method = RequestMethod.POST,name="create")
    public ObjectDataResponse<TShop> create(@RequestBody ShopCreateRequest request) {
    	String desc = "创建店铺";
    	try{
    		String mobile = BaitunWebUtils.getCurrentMobile();
    		TShopBO shop= new TShopBO();
	    	Date now = new Date();
	    	shop.setAddress(request.getAddress());
	    	shop.setContactPhone(request.getContactPhone());
    		shop.setCreateTime(now);
    		shop.setUpdateTime(now);
    		shop.setManager(Long.parseLong(mobile));
    		shop.setName(request.getName());
    		shop.setStartTime(DateUtil.format(request.getStartTime(),DateUtil.FormatD));
    		shop.setUserId(0);
	        shopService.create(shop);
	        return new ObjectDataResponse<TShop>(0,"创建店铺成功",shop);
    	}catch(Exception e){
    		logger.error(desc+"失败",e);
    		return new ObjectDataResponse<>(-1,desc+"失败.请联系客服"+e.getMessage());
    	}
    }
    
    @ApiOperation(value = "店铺详情", notes = "店铺详情")
    @RequestMapping(value = "/shop/{shopId}", method = RequestMethod.GET,name="detail")
    public ObjectDataResponse<TShop> detail(@PathVariable(value = "shopId") Integer shopId) {
    	String desc = "店铺详情";
    	try{
    		String mobile = BaitunWebUtils.getCurrentMobile();
    		TShopBO shop = new TShopBO();
    		shop.setManager(Long.parseLong(mobile));
    		shop.setId(shopId);
	        shop = shopService.queryDetail(shop);
	        return new ObjectDataResponse<TShop>(0,desc+"成功",shop);
    	}catch(Exception e){
    		logger.error(desc+"失败",e);
    		return new ObjectDataResponse<>(-1,desc+"失败.请联系客服"+e.getMessage());
    	}
    }
    
	@ApiOperation(value = "用户店铺", notes = "查询用户店铺")
	@RequestMapping(value = "/shop/account/{accountid}", method = RequestMethod.GET, name = "queryaccountshop")
	public ObjectCollectionResponse<TShop> queryShopByAccount(@PathVariable(value = "accountid") Integer accountId) {
		String desc = "我的店铺";
		try {
			List<TShop> shopList = shopService.queryShopByAccount(accountId);
			return new ObjectCollectionResponse<TShop>(0, "创建店铺成功", shopList);
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectCollectionResponse<>(-1, desc + "失败.请联系客服" + e.getMessage());
		}
	}
	
	@ApiOperation(value = "我的店铺", notes = "查询我的店铺")
	@RequestMapping(value = "/shop/myshop", method = RequestMethod.GET, name = "queryaccountshop")
	public ObjectCollectionResponse<TShopBO> queryMyShop() {
		String desc = "我的店铺";
		try {
			String mobile = BaitunWebUtils.getCurrentMobile();
//			TUserBO user = gatewayServiceAPI.me().getData();
			List<TShopBO> shopList = shopService.queryMyShop(mobile);
			return new ObjectCollectionResponse<TShopBO>(0, "创建店铺成功", shopList);
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectCollectionResponse<>(-1, desc + "失败.请联系客服" + e.getMessage());
		}
	}

}
